package com.vcms.controller.customer;

import java.util.ArrayList;
import java.util.List;

import com.vcms.controller.storemanagement.StoreController;
import com.vcms.entity.store.CashStore;
import com.vcms.entity.store.item.StoreItem;
import com.vcms.entity.store.itemtype.Coin;
import com.vcms.exception.VMCSException;
import com.vcms.utility.formatter.MoneyFormatter;

public class CoinReceiver {

	private List<Coin> coins;
	private int totalInserted;
	private TransactionController transactionController;

	public CoinReceiver(TransactionController transactionController) {
		this.transactionController = transactionController;
		coins = new ArrayList<Coin>();
	}

	public TransactionController getTransactionController() {return transactionController;}
	
	public StoreItem[] getCashStore(){
		return this.getTransactionController().getMainControler().getStoreController().getStoreItems(StoreController.CASH);
	}
	
	public void receiveCoin(double weight) throws VMCSException{
		
		//init 
		transactionController.getCustomerPanel().getInvalidCoinWarningLabel().setState(false);
		transactionController.getCustomerPanel().getCollectCoin().setValue(MoneyFormatter.toMoney(0));
		transactionController.getCustomerPanel().getCoinInput().setActive(false);
		//validation
		CashStore cashStore = (CashStore)transactionController.getMainControler().getStoreController().getStore(StoreController.CASH);
		Coin coinInserted=cashStore.findCoin(weight);
		if(coinInserted == null){
			transactionController.getCustomerPanel().getInvalidCoinWarningLabel().setState(true);
			transactionController.getCustomerPanel().getCollectCoin().setValue("Invalid Coin");
			transactionController.getCustomerPanel().getCoinInput().setActive(true);
		}else{
			coins.add(coinInserted);
			totalInserted += coinInserted.getValue();
			transactionController.getCustomerPanel().getMoneyInserted().setValue(MoneyFormatter.toMoney(totalInserted));
			transactionController.processMoneyReceived(totalInserted);
		}
	}
	
	public void continueReceive(){
		transactionController.getCustomerPanel().getCoinInput().setActive(true);
	}
	
	
	public void startReceive(){
		transactionController.getCustomerPanel().getCoinInput().setActive(true);
		transactionController.getCustomerPanel().getMoneyInserted().setValue(MoneyFormatter.toMoney(0));
		totalInserted = 0;
		coins.clear();
	}
	
	public void refundCash(){
		if(totalInserted == 0)
			return;
		
		transactionController.getCustomerPanel().getCollectCoin().setValue(MoneyFormatter.toMoney(totalInserted));
		transactionController.getCustomerPanel().getMoneyInserted().setValue(MoneyFormatter.toMoney(0));
		transactionController.getCustomerPanel().getInvalidCoinWarningLabel().setState(false);
	}
	
	public void stopReceive(){
		if(transactionController.getCustomerPanel() != null) {
			transactionController.getCustomerPanel().getCoinInput().setActive(false);
		}
	}
	
	public boolean storeCash() throws VMCSException{
		for(Coin coin : coins){
			try {
				transactionController.getMainController().getMachinaryController().storeCoin(coin);
			} catch (VMCSException e) {
				transactionController.terminateFault(this);
				throw new VMCSException("Storing Coins",e.getMessage());
			}
		}
		this.coins.clear();
		this.totalInserted = 0;
		transactionController.getCustomerPanel().getMoneyInserted().setValue(MoneyFormatter.toMoney(0));
		
		return true;
	}


}
